package com.lyshop.jinxiang.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.bean.InvestSchoolItemBean;
import com.lyshop.jinxiang.core.AppAction;
import com.lyshop.jinxiang.utils.GlideUtils;

/**
 * Created by Mr on 2018/5/10.
 */

public class InvestSchoolAdapter extends CommonAdapter<InvestSchoolItemBean> {
    private AppAction appAction;
    private InvestSchoolListener mListener;
    private boolean isShowImageOnly;


    public InvestSchoolAdapter(Context context, InvestSchoolListener listener) {
        super(context);
        mListener = listener;
        appAction=((MyApplication)context.getApplicationContext()).getAppAction();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_invest_school_new, parent, false);
            viewHolder = new ItemViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ItemViewHolder) convertView.getTag();
        }
        viewHolder.setData(position);
        return convertView;
    }

    class ItemViewHolder implements View.OnClickListener {
        private View rl_parent;
        private RelativeLayout rl_content;
        private ImageView iv_image;
        private ImageView iv_content_img;
        private TextView tv_title;
        private TextView tv_content;
        private InvestSchoolItemBean itemBean;

        ItemViewHolder(View itemView) {
            rl_parent = itemView.findViewById(R.id.rl_parent);
            rl_content = itemView.findViewById(R.id.rl_content);
            iv_content_img = (ImageView) itemView.findViewById(R.id.iv_content_img);
            iv_image = (ImageView) itemView.findViewById(R.id.iv_image);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            rl_parent.setOnClickListener(this);
        }

        private void setData(int position) {
            itemBean = getItem(position);
            if (itemBean != null) {
                GlideUtils.showImageOptions(mContext, itemBean.getImg(), iv_image);
                GlideUtils.showImageOptions(mContext, itemBean.getImg(), iv_content_img);
                tv_title.setText(itemBean.getTitle());
                tv_content.setText(itemBean.getDesc());
            }

            if(isShowImageOnly){
                iv_content_img.setVisibility(View.VISIBLE);
                rl_content.setVisibility(View.GONE);
            }else{
                iv_content_img.setVisibility(View.GONE);
                rl_content.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onClickItem(itemBean);
            }
        }
    }

    public interface InvestSchoolListener {
        void onClickItem(InvestSchoolItemBean item);
    }

    public void setShowImageOnly(boolean showImageOnly) {
        isShowImageOnly = showImageOnly;
        notifyDataSetChanged();
    }
}
